/*
* Copyright (c) 2008 michiyasu wada
* http://www.seyself.com/
* 
* Distributed under The MIT License.
* [http://www.opensource.org/licenses/mit-license.php]
*/

import mx.events.EventDispatcher;

class com.seyself.utils.StageUtil
{
    
    /// PUBLIC STATIC METHOD
    
    public static function init( width:Number, height:Number, maxSize:Object, minSize:Object ):Void
    {
        manager = new StageUtil( width , height , maxSize , minSize );
        delete StageUtil.init;
    }
    
    public static function addEventListener( type:String , object:Object ):Void
    {
        dispatcher.addEventListener( type , object );
    }
    
    public static function removeEventListener( type:String , object:Object ):Void
    {
        dispatcher.removeEventListener( type , object );
    }
    
    public static function eventRequest():Void
    {
        stageObj.onResize();
    }
    
    public static function getSizeInfo():Object
    {
        return { 
                width: manager._width ,
                height: manager._height ,
                left: manager._left ,
                right: manager._right ,
                top: manager._top ,
                bottom: manager._bottom ,
                center: manager._center ,
                middle: manager._middle 
                };
    }
    
    
    /// PRIVATE _____________________________________________________________
    
    private static var manager:StageUtil;
    private static var stageObj:Object;
    private static var dispatcher:Object;
    
    private function StageUtil( width:Number, height:Number, maxSize:Object, minSize:Object )
    {
        var loc = this;
        stageObj = {};
        dispatcher = {};
        defaultWidth = width;
        defaultHeight = height;
        if( maxSize != undefined ){
            if( maxSize.width != undefined && maxSize.height != undefined ){
                maxWidth = maxSize.width;
                maxHeight = maxSize.height;
            }
        }
        if( minSize != undefined ){
            if( minSize.width != undefined && minSize.height != undefined ){
                minWidth = minSize.width;
                minHeight = minSize.height;
            }
        }
        EventDispatcher.initialize( dispatcher );
        stageObj.onResize = function()
        {
            loc.resizeHandler();
            StageUtil.dispatcher.dispatchEvent({ 
                type:'resize' ,
                width: loc._width ,
                height: loc._height ,
                left: loc._left ,
                right: loc._right ,
                top: loc._top ,
                bottom: loc._bottom ,
                center: loc._center ,
                middle: loc._middle 
                });
        }
        Stage.addListener( stageObj );
        stageObj.onResize();
    }
    
    private var defaultWidth:Number = 550;
    private var defaultHeight:Number = 400;
    private var maxWidth:Number = 10000;
    private var maxHeight:Number = 10000;
    private var minWidth:Number = 0;
    private var minHeight:Number = 0;
    
    private var _width;
    private var _height;
    private var _top;
    private var _bottom;
    private var _right;
    private var _left;
    private var _center;
    private var _middle;
    
    private function resizeHandler()
    {
        var x,y;
        var w = Stage.width;
        var h = Stage.height;
        if( Stage.align.indexOf("L")>=0 ){
            x = 0;
        } else if( Stage.align.indexOf("R")>=0 ){
            x = w-defaultWidth;
        } else {
            x = -(w-defaultWidth)/2;
        }
        if( Stage.align.indexOf("T")>=0 ){
            y = 0;
        } else if( Stage.align.indexOf("B")>=0 ){
            y = h-defaultHeight;
        } else {
            y = -(h-defaultHeight)/2;
        }
        _width = Math.min( maxWidth , Math.max( minWidth , w ));
        _height = Math.min( maxHeight , Math.max( minHeight , h ));
        _left = Math.min( (defaultWidth-minWidth)/2 , Math.max( (defaultWidth-maxWidth)/2 , x ));
        _right = _left+_width;
        _top = Math.min( (defaultHeight-minHeight)/2 , Math.max( (defaultHeight-maxHeight)/2 , y ));
        _bottom = _top+_height;
        _center = _left+_width/2;
        _middle = _top+_height/2;
    }
    
}
